"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isObject = exports.isArray = exports.isString = exports.TableConfig = exports.ValidationError = exports.PageResponse = void 0;
exports.anyToEntity = anyToEntity;
exports.anyToEntities = anyToEntities;
/**
 * 分页响应类
 */
class PageResponse {
    constructor() {
        this.totalElements = 0;
        this.page = 0;
        this.size = 10;
        this.content = [];
        this.orderBy = 'id';
        this.direction = 'desc';
        this.totalPages = 0;
    }
    setTotalElements(totalElements) {
        this.totalElements = totalElements;
        this.totalPages = Math.ceil(this.totalElements / this.size);
    }
    /**
     * 从请求对象创建分页响应对象
     */
    static of(req) {
        const { page, size, orderBy, direction } = PageResponse.getPageAndSize(req);
        const pageResponse = new PageResponse();
        pageResponse.page = page;
        pageResponse.size = size;
        pageResponse.orderBy = orderBy;
        pageResponse.direction = direction;
        return pageResponse;
    }
    /**
     * 从请求对象获取分页参数
     */
    static getPageAndSize(req) {
        const page = parseInt(req.query.page || '0');
        const size = parseInt(req.query.size || '10');
        const orderBy = req.query.orderBy || 'id';
        const direction = (req.query.direction || 'asc');
        return { page, size, orderBy, direction };
    }
}
exports.PageResponse = PageResponse;
/**
 * 验证错误类
 */
class ValidationError extends Error {
    constructor(message) {
        super(message);
        this.name = 'ValidationError';
    }
}
exports.ValidationError = ValidationError;
/**
 * 表配置类
 */
class TableConfig {
    constructor(table, columnSet) {
        this.columnSet = [];
        this.idName = 'id';
        this.idAutoCreate = true;
        this.jsonColumn = [];
        this.createTime = '';
        this.updateTime = '';
        this.createReturn = true;
        this.parents = [];
        this.table = table;
        this.columnSet = columnSet;
    }
}
exports.TableConfig = TableConfig;
// 工具函数
const isString = (val) => typeof val === 'string';
exports.isString = isString;
const isArray = (val) => Array.isArray(val);
exports.isArray = isArray;
const isObject = (val) => val !== null && typeof val === 'object' && !Array.isArray(val);
exports.isObject = isObject;
function anyToEntity(val, newEntity) {
    if (!val || typeof val !== 'object') {
        return newEntity();
    }
    const entity = newEntity();
    Object.keys(entity).forEach(key => {
        if (key in val) {
            // @ts-ignore
            entity[key] = val[key];
        }
    });
    return entity;
}
function anyToEntities(values, newEntity) {
    if (!values || !Array.isArray(values)) {
        return [];
    }
    return values.map(value => anyToEntity(value, newEntity));
}
